//
//  LSTextField.h
//  FreshMember
//
//  Created by qyc on 2017/8/22.
//  Copyright © 2017年 LeShu. All rights reserved.
//

#import "NSDate+String.h"

@implementation NSDate (String)

/**
 格式化为字符串
 */
-(NSString*) toString:(NSString *)format {
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSString* strRet = [dateFormatter stringFromDate:self];
    
    return strRet;
}

/**
 将格式化字符转化为NSDate
 */
+(NSDate*) fromString:(NSString *)strDate withFormat:(NSString *)format {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:format];
    NSDate *date=[formatter dateFromString:strDate];
    return date;
}

//将某个时间戳转化成 时间

#pragma mark - 将某个时间戳转化成 时间
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

/**
 *  比较时间大小
 
 *  @return  1 //date02比date01大 -1 //date02比date01小
 */
+ (int)p_compareDate:(NSString*)date01 withDate:(NSString*)date02 andFormatter:(NSString *)format
{
    int isLittle;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:format];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending:
            isLittle = 1;
            break;
            //date02比date01小
        case NSOrderedDescending:
            isLittle = -1;
            break;
            //date02=date01
        case NSOrderedSame:
            isLittle = 0;
            break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return isLittle;
}

/**
 获取当地时间 年月日
 */
+ (NSString *)getCurrentTimeFormatter:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}


/**
 将字符串转成NSDate类型
 */
+ (NSDate *)dateFromString:(NSString *)dateString {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    return destDate;
}


/**
 传入今天的时间，返回明天的时间
 @param aDate 今天的时间
 @return 返回明天的时间
 */
+ (NSString *)GetTomorrowDay:(NSDate *)aDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+1)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd"];
    return [dateday stringFromDate:beginningOfWeek];
}

/**
 当前时间和后台返回的时间比较方法，返回一个int类型的数据
 
 @param oneDay 当前时间
 @param anotherDay 返回时间
 @return nil
 */
+ (int)compareCurrentTimeDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //这个时间格式需要和当前得到时间格式相同才能做比较
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        //当前时间大于返回时间
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        //当前时间小于返回时间
        return -1;
    }
    //NSLog(@"Both dates are the same");
    //完全相等（NSOrderedDescending，NSOrderedAscending，NSOrderedSame这三个是一个枚举类型）
    return 0;
}

/**
 获取现在的时间戳
 */
+ (NSString *)getNSTimeInterval
{
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)interval];
    return timeString;
}

/**
 * 比较两个时间相差几天
 *
 *  @return 相差天数
 */
+ (NSInteger)getDifferenceByEndDate:(NSString *)endDate beginDate:(NSString *)beginDate{
    //获得开始时间
    //实例化一个NSDateFormatter对象
    NSDateFormatter *beginDateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [beginDateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *begin = [beginDateFormatter dateFromString:beginDate];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *end = [dateFormatter dateFromString:endDate];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:begin  toDate:end  options:0];
    return [comps day];
}


/**
 获取传入日期是周几

 @param format 传入例如"2018-05-09"或者"2016/05/09"格式的字符串即可返回日期是周几
 @return 周几
 */
+ (NSString *)weekDayStr:(NSString *)format {
    
    NSString *weekDayStr = nil;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    if(format.length>=10) {
        NSString *nowString = [format substringToIndex:10];
        NSArray *array = [nowString componentsSeparatedByString:@"-"];
        if(array.count==0) {
            array = [nowString componentsSeparatedByString:@"/"];
        }
        
        if(array.count>=3) {
            NSInteger year = [[array objectAtIndex:0] integerValue];
            NSInteger month = [[array objectAtIndex:1] integerValue];
            NSInteger day = [[array objectAtIndex:2] integerValue];
            [comps setYear:year];
            [comps setMonth:month];
            [comps setDay:day];
        }
    }
    //日历
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //获取传入date
    NSDate *_date = [gregorian dateFromComponents:comps];
    
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date];
    NSInteger week = [weekdayComponents weekday];
    switch(week) {
        case 1:
            weekDayStr =@"周日";
            break;
        case 2:
            weekDayStr =@"周一";
            break;
        case 3:
            weekDayStr =@"周二";
            break;
        case 4:
            weekDayStr =@"周三";
            break;
        case 5:
            weekDayStr =@"周四";
            break;
        case 6:
            weekDayStr =@"周五";
            break;
        case 7:
            weekDayStr =@"周六";
            break;
        default:
            weekDayStr =@"";
            break;
    }
    return weekDayStr;
}

@end
